import React from "react";
import { connect } from "react-redux";
import { messages } from "share/common";
import { Steps } from "antd";
import expenseReportService from 'containers/expense-report/expense-report.service';
import Condition from "components/condition";
import baseService from 'share/base.service'
const { If, Else } = Condition;
const { Step } = Steps;

/**
 * @param {*} info
 * @param {*} approvalChains
 *
 * @return {number} currentStatus
 */
export function getExpenseStepStatus(info, approvalChains) {
  const { status, receiveStatus } = info;
  let current = 0;
  if ([1001, 10011001, 10011002, 10011003, 10011004].includes(status)) {
    //编辑
    current = 0;
  } else if (status === 1008 || status === 1007 || status === 1004) {
    // 付款
    if (receiveStatus !== 0) {
      // TES-2019
    if (status === 1004 && info.realPaymentAmount === 0) {
      current = 6;
    } else {
      current = 5; // 付款
    }
    } else {
      current = 3; // 收件
    }
  } else if (status === 1005) {
    // 结束
    if (receiveStatus !== 0) {
      current = 6; // 结束
    } else {
      current = 3; // 收件
    }
  } else if (status === 1002) {
    // 审批中
    if (
      approvalChains[0] &&
      approvalChains[0].brmsRemark &&
      approvalChains[0].brmsRemark === "财务预审"
    ) {
      current = 2;
    } else {
      current = 1;
    }
  } else if (status === 1010) {
    // 预审通过
    if (receiveStatus !== 0) {
      current = 4; // 审核
    } else {
      current = 3; // 收件
    }
  } else if (status === 1003) {
    current = 2;
  }

  return current;
}

class ExpenseDetailStep extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      status: props.info.status,
      receiveStatus: props.info.receiveStatus,
      type: props.type,
      approvalChains: props.approvalChains,
      stepList: [],
      current: 0,
      expenseStepList: [
        { title: "编辑", status: 'wait'},
        { title: "审批", status: 'wait'},
        { title: "预审", status: 'wait'},
        { title: "收件", status: 'wait'},
        { title: "审核", status: 'wait'},
        { title: "结束", status: 'wait'}
      ],
      honestInfo: undefined
    };
  }
  componentDidMount() {
    this.getHonestInfo(this.props.info)
    // this.getStepStatus();
  }

  getHonestInfo = (info) => {
    const { applicantOID, applicantJobId } = info;
    baseService.getHonestInfo(applicantOID, applicantJobId).then(res => {
      this.setState({ honestInfo: res.data || {} }, () => {
        this.getStepStatus();
      })
    })
  };

  getStepStatus = () => {
    const { type, status, approvalChains, receiveStatus } = this.state;
    let current = 0;
    if (type === "expense") {
      this.setState({
        current: getExpenseStepStatus(this.props.info, approvalChains),
      });
      console.log("this.props.info", this.props.info)
      expenseReportService.getPayFlow(this.props.info.expenseReportOID).then(res => {
        console.log("res=======", res)
        let current = getExpenseStepStatus(this.props.info, approvalChains);
        let score = this.state.honestInfo.score;
        let arr = this.state.expenseStepList;
        let formCode = this.props.info.formCode;
        let { no, isPaid, turnOff } = res.data;
        if ([1, 2, 3].includes(no)) {
          console.log("current", current)
          if (no === 1) {
            // 编辑 - 审批 - 预审 - 收件 - 审核 - 付款 - 结束
            if (current < 5) {
              arr.map((item, index) => {
                if (index < current) {
                  item.status = 'finish';
                } else if (index === current) {
                  item.status = 'process';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(5, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(5, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[5].status = 'wait';
                }
              }
            } else if (current === 5) {
              arr.map((item, index) => {
                if (index < current) {
                  item.status = 'finish';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(5, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(5, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[5].status = 'wait';
                }
              }
            } else if (current === 6) {
              arr.map((item, index) => {
                if (index < current - 1) {
                  item.status = 'finish';
                } else {
                  item.status = 'process';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(5, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(5, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[5].status = 'wait';
                }
              }
            }
          }
          if (no === 2) {
            // 编辑 - 审批 - 预审 - 付款 - 收件 - 审核 - 结束
            if (current < 5) {
              arr.map((item, index) => {
                if (index < current) {
                  item.status = 'finish';
                } else if (index === current) {
                  item.status = 'process';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(3, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(3, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[3].status = 'wait';
                }
              }
            } else if (current === 5) {
              arr.map((item, index) => {
                if (index < current) {
                  item.status = 'finish';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(3, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(3, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[3].status = 'wait';
                }
              }
            } else if (current === 6) {
              arr.map((item, index) => {
                if (index < current - 1) {
                  item.status = 'finish';
                } else {
                  item.status = 'process';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(3, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(3, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[3].status = 'wait';
                }
              }
            }
          }
          if (no === 3) {
            // 编辑 - 审批 - 付款 - 预审 - 收件 - 审核 - 结束
            if (current < 5) {
              arr.map((item, index) => {
                if (index < current) {
                  item.status = 'finish';
                } else if (index === current) {
                  item.status = 'process';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(2, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(2, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[2].status = 'wait';
                }
              }
            } else if (current === 5) {
              arr.map((item, index) => {
                if (index < current) {
                  item.status = 'finish';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(2, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(2, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[2].status = 'wait';
                }
              }
            } else if (current === 6) {
              arr.map((item, index) => {
                if (index < current - 1) {
                  item.status = 'finish';
                } else {
                  item.status = 'process';
                }
              })
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(2, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(2, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[2].status = 'wait';
                }
              }
            }
          }
        } else if (no === 0) {
           // 编辑 - 审批 - 预审 - 收件 - 审核 - 结束
          if (current < 5) {
            arr.map((item, index) => {
              if (index < current) {
                item.status = 'finish';
              } else if (index === current) {
                item.status = 'process';
              }
            })
            if (score >= 80) {
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(2, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(2, 0, { title: '付款', status: 'wait'})
                }
                if (turnOff) {
                  arr[2].status = 'wait';
                }
              }
            } else if (score >= 70) {
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(3, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(3, 0, { title: '付款', status: 'wait'})
                }
                if (turnOff) {
                  arr[3].status = 'wait';
                }
              }
            } else {
              if (!['BX002', 'BX004'].includes(formCode)) {
                if (isPaid) {
                  arr.splice(5, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(5, 0, { title: '付款', status: 'wait'})
                }
                if (turnOff) {
                  arr[5].status = 'wait';
                }
              }
            }
          } else if (current === 5) {
            arr.map((item, index) => {
              if (index < current) {
                item.status = 'finish';
              }
            })
            if (!['BX002', 'BX004'].includes(formCode)) {
              if (score >= 80) {
                if (isPaid) {
                  arr.splice(2, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(2, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[2].status = 'wait';
                }
              } else if (score >= 70) {
                if (isPaid) {
                  arr.splice(3, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(3, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[3].status = 'wait';
                }
              } else {
                if (isPaid) {
                  arr.splice(5, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(5, 0, { title: '付款', status: 'process'})
                }
                if (turnOff) {
                  arr[5].status = 'wait';
                }
              }
            }
          } else if (current === 6) {
            arr.map((item, index) => {
              if (index < current - 1) {
                item.status = 'finish';
              } else {
                item.status = 'process';
              }
            })
            if (!['BX002', 'BX004'].includes(formCode)) {
              if (score >= 80) {
                if (isPaid) {
                  arr.splice(2, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(2, 0, { title: '付款', status: 'wait'})
                }
                if (turnOff) {
                  arr[2].status = 'wait';
                }
              } else if (score >= 70) {
                if (isPaid) {
                  arr.splice(3, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(3, 0, { title: '付款', status: 'wait'})
                }
                if (turnOff) {
                  arr[3].status = 'wait';
                }
              } else {
                if (isPaid) {
                  arr.splice(5, 0, { title: '付款', status: 'finish'})
                } else {
                  arr.splice(5, 0, { title: '付款', status: 'wait'})
                }
                if (turnOff) {
                  arr[5].status = 'wait';
                }
              }
            }
          }
        }
        console.log('arr=====', arr)
        this.setState({
          expenseStepList: arr
        })
      })
    } else {
      if ([10011001, 10011002, 10011003, 10011000].includes(status)) {
        //编辑
        current = 0;
      } else if (status === 1011 || status === 1004) {
        // 付款
        current = 3;
      } else if (status === 1005 || status === 1006 || status === 1007 || status === 1009 || status === 1012) {
        // 结束
        current = 4;
      } else if (status === 1002) {
        // 审批中
        current = 1;
      } else if (status === 1003) {
        // 审核
        current = 2;
      }
      this.setState({
        current,
      });
    }
  };
  render() {
    const { current, type, expenseStepList } = this.state;
    return (
      <div style={{ margin: 20 }}>
        {type === "expense" ? <Steps>
          {expenseStepList.map(item => (
            <Step title={item.title} status={item.status} />
          ))}
        </Steps> : <Steps current={current}>
          {["编辑", "审批", "审核", "付款", "结束"].map((e) => (
                <Step title={e} />
              ))}
        </Steps>}
        {/* <Steps current={current}>
          {type === "expense"
            ? [
               "编辑",
               "审批",
               "预审",
               "收件",
                "审核",
                "付款",
                "结束",
              ].map((e) => <Step title={e} />)
            : ["编辑", "审批", "审核", "付款", "结束"].map((e) => (<Step title={e} />
          ))}
          </Steps> */}
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {};
}

export default connect(mapStateToProps)(ExpenseDetailStep);
